package com.htjc.integral.activity.controller;

import java.text.SimpleDateFormat;

import javax.print.DocFlavor.STRING;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.activity.service.IIntegralLotteryService;
import com.htjc.integral.activity.util.JsonUtil;
import com.htjc.integral.activity.util.ResultModel;
import com.htjc.integral.activity.util.enums.ActivityReturn;
import com.htjc.integral.common.annotation.ExecuteTime;
import com.htjc.integral.common.annotation.InvokeLog;
import com.htjc.integral.common.util.StringUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author zhangjiajia
 *
 */
@Slf4j(topic = "jfLog")
@RestController
@RequestMapping(value = "/ad/lottery")
public class IntegralLotteryController {
	@Autowired
	private IIntegralLotteryService iIntegralLotteryService; 
	/**
	 * 积分抽奖首页
	 * @param json(promotionId:活动id,userId：用户id,userName)
	 * @return json
	 */
	@ExecuteTime
	@InvokeLog(value="积分抽奖首页",params={"IP","CHANNEL_NO","UNION_ID","PROMOTION_ID","USER_NAME"})
	@RequestMapping(value = {"/init"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String init(@RequestBody String params){
		log.debug("账户中心查询接口====开始====参数:token：{}", params);
		String result = null;
		try {
			//1、校验参数
			log.info("校验参数=======================》开始《===================");
			JSONObject json = JSON.parseObject(params);
			
			//校验业务ID
			if (StringUtil.isNull(json.getString("PROMOTION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.PROMOTION_ID_NULL))).toString();
			}
			//校验登录账户ID
			if (StringUtil.isNull(json.getString("UNION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.USER_ID_NULL))).toString();
			}
			
			//2、调用积分抽奖首页信息
			JSONObject payChangeIntegral = iIntegralLotteryService.init(json);
			result = payChangeIntegral.toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//3、返回结果
		log.debug("账户中心查询接口====完成====输入参数:{},返回参数:{}", params, result);
		return result;
	}
	/**
	 * 积分抽奖
	 * @param json(promotionId:活动id,userId：用户id,TelPhone电话)
	 * @return json
	 */
	@ExecuteTime
	@InvokeLog(value="积分抽奖",params={"IP","CHANNEL_NO","UNION_ID","PROMOTION_ID","USER_NAME","TELPHONE"})
	@RequestMapping(value = {"/drawLottery"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String drawLottery(@RequestBody String params){
		log.debug("账户中心查询接口====开始====参数:token：{}", params);
		String result = null;
		try {
			//1、校验参数
			log.info("校验参数=======================》开始《===================");
			JSONObject json = JSON.parseObject(params);
			
			//校验业务ID
			if (StringUtil.isNull(json.getString("PROMOTION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.PROMOTION_ID_NULL))).toString();
			}
			//校验登录账户ID
			if (StringUtil.isNull(json.getString("UNION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.USER_ID_NULL))).toString();
			}
			
			//2、调用积分抽奖首页信息
			JSONObject payChangeIntegral = iIntegralLotteryService.drawLottery(json);
			result = payChangeIntegral.toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//3、返回结果
		log.debug("账户中心查询接口====完成====输入参数:{},返回参数:{}", params, result);
		return result;
	}
	/**
	 * 积分抽奖验证
	 * @param json(promotionId:活动id,userId：用户id,TelPhone电话)
	 * @return json
	 */
	@ExecuteTime
	@InvokeLog(value="积分抽奖",params={"IP","CHANNEL_NO","UNION_ID","PROMOTION_ID","USER_NAME"})
	@RequestMapping(value = {"/getLotteryCheck"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String getLotteryCheck(@RequestBody String params){
		log.debug("账户中心查询接口====开始====参数:token：{}", params);
		String result = null;
		try {
			//1、校验参数
			log.info("校验参数=======================》开始《===================");
			JSONObject json = JSON.parseObject(params);
			
			//校验业务ID
			if (StringUtil.isNull(json.getString("PROMOTION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.PROMOTION_ID_NULL))).toString();
			}
			//校验登录账户ID
			if (StringUtil.isNull(json.getString("UNION_ID"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.ActivityReturnError(ActivityReturn.USER_ID_NULL))).toString();
			}
			
			//2、调用积分抽奖首页信息
			JSONObject payChangeIntegral = iIntegralLotteryService.getLotteryCheck(json);
			result = payChangeIntegral.toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//3、返回结果
		log.debug("账户中心查询接口====完成====输入参数:{},返回参数:{}", params, result);
		return result;
	}
	/**
	 * 我的中奖记录
	 * @param param
	 * @return
	 */
	@ExecuteTime
	@InvokeLog(value="我的中奖记录",params={"IP","CHANNEL_NO","UNION_ID","PROMOTION_ID","USER_NAME","CURPAGE","PAGESIZE"})
	@RequestMapping(value = {"/getMyLotterySuccessInfo"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String getMyLotterySuccessInfo(@RequestBody String params) {
		log.debug("账户中心查询接口====开始====参数:token：{}", params);
		String result = null;
		try {
			//1、校验参数
			log.info("校验参数=======================》开始《===================");
			JSONObject json = JSON.parseObject(params);
			//2、调用积分抽奖首页信息
			JSONObject payChangeIntegral = iIntegralLotteryService.getMyLotterySuccessInfo(json);
			result = payChangeIntegral.toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//3、返回结果
		log.debug("账户中心查询接口====完成====输入参数:{},返回参数:{}", params, result);
		return result;
	}
	public static void main(String[] args) {
		String a=null;
		System.out.println(a.toString());
	}

}
